home *** CD-ROM | disk | FTP | other *** search
/ PC Media 22 / PC MEDIA CD22.iso / share / prog / datalib2 / familys.cpp < prev    next >
C/C++ Source or Header  |  1995-08-17  |  2KB  |  63 lines

  1.  
  2. /*
  3.    Demonstration of Subtotals,
  4.  
  5.    Short program to update average age for each family in familys.dbf
  6.    The average age is a field in each record which will indicate how far
  7.    away each family member is in age from the average age in that family
  8.  
  9.    Index expression in familys.ndx is : 
  10.    
  11.      UPPER(SURNAME)
  12.  
  13.    Thus all family members will be grouped together
  14. */
  15.  
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include "database.hpp"
  21.  
  22. void main()
  23. {
  24.  char ws[128];                   // Workspace
  25.  
  26.  database db("familys","familys");              // Open database
  27.  record rec(db);                                        // Current record
  28.  record first(db);                                                              // Used to save position
  29.  int fn=db.getfield("avgage")->getnumber();             // Field no. avgage field
  30.  
  31.  int sel=rec.select(FIRST);             // Select first record
  32.  do
  33.  {
  34.   char surname[128];            // Holds current surname
  35.   double avgage=0;                      // Holds average age
  36.   first=rec;                            // Save position
  37.   int ssel=sel;                 // save sel at current position
  38.   int trec=0;                           // Total no. of records in patrol
  39.  
  40.   strcpy(surname,rec.getfield("surname"));          // Copy in patrol name
  41.  
  42.   while(!sel && !strcmp(surname,rec.getfield("surname"))) // Until new name
  43.   {
  44.    avgage+=atof(rec.getfield("age"))+atof(rec.getfield("agem"))/12;
  45.    trec++;
  46.    sel=rec.select(NEXT);                        // On to next record
  47.   }
  48.   avgage/=trec;
  49.  
  50.   while(!ssel && !strcmp(surname,first.getfield("surname")))  // Round again
  51.   {
  52.    double age;
  53.  
  54.    age=atof(first.getfield("age"))+atof(first.getfield("agem"))/12;
  55.    first.setfield(fn,int(age-avgage));
  56.    first.write();                       // Write the new record
  57.    ssel=first.select(NEXT);
  58.   }
  59.   printf("\nAverage age of %s is %f",surname,avgage);
  60.  }
  61.  while(!sel);
  62. }
  63.